package com.cr.crsystem.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.crsystem.common.base.constant.CommonConstant;
import com.cr.crsystem.common.utils.Result;
import com.cr.crsystem.system.entity.Car;
import com.cr.crsystem.system.service.CarService;
import com.cr.crsystem.system.vo.CarVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author su
 * @since 2022-06-21
 */
@CrossOrigin
@Api(tags = "车辆管理")
@Slf4j
@RestController
@RequestMapping("/system/car")
public class CarController {

    @Autowired
    private CarService carService;


    @ApiOperation("新增车辆")
    @PostMapping("/add")
    public Result<Car> add(@RequestBody Car car) {
        Result<Car> result = new Result<>();
        car.setStatus(CommonConstant.APPROVE_STATUS_0);//设置待审核
        car.setIsRent(CommonConstant.NOT_REND);//未出租
        boolean save = carService.save(car);
        if (save) {
            result.setResult(car);
            result.success("添加成功");
        } else {
            result.error20001("操作失败");
        }
        return result;
    }

    @ApiOperation("修改车辆")
    @PostMapping("/edit")
    public Result<Car> edit(@RequestBody Car car) {
        Result<Car> result = new Result<>();
        if (car != null) {
            if (carService.edit(car)) {
                result.setResult(car);
                return result.success("修改车辆成功");
            }
        }
        return result.error20001("修改车辆失败");
    }

    @ApiOperation("删除车辆")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Integer id) {
        if (id!=0) {
            if (carService.deleteCar(id)) {
                return Result.OK().success("删除车辆成功");
            }
        }
        return Result.error("删除车辆失败");
    }

    @ApiOperation("查询所有车辆")
    @GetMapping("/queryAll")
    public Result<?> queryAll() {
        List<Car> carList = carService.list();
        return Result.OK(carList);
    }


    @ApiOperation("查询个人所有车辆")
    @GetMapping("/queryPersonCar")
    public Result<?> queryPersonCar(@RequestParam(name = "userId", required = true) Integer userId,
                                    @RequestParam(name = "status", required = false) String status) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Car::getUserId, userId);
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.lambda().eq(Car::getStatus, status);
        }
        List<Car> carList = carService.list(queryWrapper);
        return Result.OK(carList);
    }


    @ApiOperation("查询个人车辆状态")
    @GetMapping("/queryPublishPersonCar")
    public Result<?> queryPublishPersonCar(@RequestParam(name = "userId", required = true) Integer userId,
                                           @RequestParam(name = "status", required = false) String status) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Car::getUserId, userId);

        if (!StringUtils.isEmpty(status)) {
            queryWrapper.lambda().ne(Car::getStatus, status);
        }
        List<Car> carList = carService.list(queryWrapper);
        return Result.OK(carList);
    }


    @ApiOperation("查询未审核车辆")
    @GetMapping("/queryUnApproveCar")
    public Result<?> queryUnApproveCar(@RequestParam(name = "status", required = true) String status) {
        List<CarVo> carVos = carService.queryUnApproveCar(status);
        return Result.OK(carVos);
    }

    @ApiOperation("查询已审核车辆")
    @GetMapping("/queryApproveCar")
    public Result<?> queryApproveCar(@RequestParam(name = "status", required = true) String status) {
        List<CarVo> carVos = carService.queryApproveCar(status.split(","));
        return Result.OK(carVos);
    }

}

